﻿using System.ComponentModel;
using K9Nano.Application;
using K9Nano.Application.Dtos;
using K9Nano.Domain.Entities;
using Microsoft.AspNetCore.Mvc;

namespace K9Nano.AspNetCore.Controllers;

[Route("api/[controller]")]
public abstract class CurdControllerBase<TEntity, Tservice, TCreateDto, TEditDto, TItemDto, TQueryDto>(Tservice service)
    : ApiControllerBase
    where TEntity : class, IEntity
    where TEditDto : IKeyDto
    where TQueryDto : PagedQueryDto<TEntity>
    where Tservice : ICurdAppServiceBase<TEntity, TCreateDto, TEditDto, TItemDto, TQueryDto>
{
    /// <summary>
    /// 创建数据参数
    /// </summary>
    /// <param name="createDto">创建数据参数对象</param>
    /// <returns>创建后的数据对象</returns>
    [HttpPost]
    public virtual async Task<TItemDto> Create(TCreateDto createDto)
    {
        var item = await service.CreateAsync(createDto, HttpContext.RequestAborted);
        return item;
    }

    /// <summary>
    /// 根据ID获取数据
    /// </summary>
    /// <param name="id">主键ID</param>
    /// <returns>数据对象</returns>
    [HttpGet("{id}")]
    public virtual async Task<TItemDto?> Get(long id)
    {
        var item = await service.GetAsync(id, HttpContext.RequestAborted);
        return item;
    }

    /// <summary>
    /// 更新数据参数
    /// </summary>
    /// <returns>更新后的数据对象</returns>
    [HttpPut]
    public virtual async Task<TItemDto> Update(TEditDto updateDto)
    {
        var item = await service.UpdateAsync(updateDto, HttpContext.RequestAborted);
        return item;
    }

    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="id">主键ID</param>
    /// <returns>是否删除成功</returns>
    [HttpDelete("{id}")]
    public virtual async Task<bool> Delete(long id)
    {
        var result = await service.DeleteAsync(id, HttpContext.RequestAborted);
        return result;
    }

    /// <summary>
    /// 分页查询数据
    /// </summary>
    /// <param name="queryDto">查询参数</param>
    /// <returns></returns>
    [HttpGet("query")]
    public virtual async Task<PagedResultDto<TItemDto>> Query([FromQuery] TQueryDto queryDto)
    {
        var result = await service.QueryAsync(queryDto, HttpContext.RequestAborted);
        return result;
    }

    /// <summary>
    /// 获取所有数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("all")]
    [Description("获取所有数据")]
    public virtual async Task<IEnumerable<TItemDto>> GetAll()
    {
        var result = await service.GetAllAsync(HttpContext.RequestAborted);
        return result;
    }
}